var pageNow = 1
SPA_RESOLVE_INIT = function (transition) {
    if ($.getQueryString("courseId") == undefined) return false;
    var html = '<div class="commonRight-top-select mb-10 f-14 mt-15">\n'+
        '添加方式 : '+
        '<select onchange="inputTypeChanges(this)">'+
        '<option value="0">手动添加</option>'+
        '<option value="1">文件添加</option>'+
        '</select>\n'+
        '<div id="addInputs" class="thefile" style="display: inline">'+
        '<input type="number" id="input-adduser" class="rad-3 ml-10 f-14">'+
        '<a href="javascript:;" class="ui-btn text-center c-fff f-14 bg-169 rad-3 ml-10 " onclick="addCourseStudent()">确认添加</a>'+
        '</div>'+
        '</div>\n'+
        '<table class="Table_common courseStudentsListTab mt-20">\n'+
        '<thead>\n'+
        '<tr>\n'+
        '<td>用户名</td>\n'+
        '<td>昵称</td>\n'+
        '<td>分数</td>\n'+
        '<td>操作</td>\n'+
        '</tr>\n'+
        '</thead>\n'+
        '<tbody id="courseStudentListTabs"></tbody>\n'+
        '</table> '+
        '<div class="paging-area-box mt-10 magin-auto">\n'+
        '<div class="paging-area float-left" id="pagination"></div>\n'+
        '<span class="pageNum float-right f-14">共<em>0</em>条数据</span>\n'+
        '</div>';

    html += '<style>\n'+
        'table{'+
        'table-layout: fixed'+
        '}'+
        '\n'+
        '.courseStudentsListTab tr td:nth-child(2){\n'+
        'border-left:1px solid #333;\n'+
        'border-right:1px solid #333;\n'+
        '}\n'+
        '.courseStudentsListTab tr td:nth-child(3){\n'+
        'border-right:1px solid #333;\n'+
        '}\n'+
        '\n'+
        '</style>';

    getHref_path();
    $(".menu ul").hide();
    $(".menu ul").eq(1).show();
    document.getElementById("ui-right-content").innerHTML = html;
    $("#addUser").hide();
    courseStudentListFun(pageNow);
}

function courseStudentListFun(pageNo) {
    var id = window.document.getElementById("courseStudentListTabs");

    $.ajax({
        url: get_courseStudent_interface + '?currentPage='+ pageNo,
        type: 'post',
        dataType: 'json',
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        data: {
            courseId: $.getQueryString("courseId"),
            currentPage: pageNo,
        },
        success: function (data) {
            if (data.code == 200) {
                inner_courseStudentTbody(data, id, courseStudentListFun);
            } else {
                $.message(data.msg);
            }
        },
        error: function (error) {
            parent.popMessage("链接超时，请刷新...");
        }
    });
}

function inner_courseStudentTbody(data, id, func) {
    var page = data.data.num;
    var currentPage = data.data.currentPage;
    var tml = "";
    pageNow = currentPage
    $(".pageNum em").html(page);
    if (page > 0) {
        setPagination(page, currentPage, func);
        var lists = data.data.list;
        $('#pagination').show();
        for (x in lists) {
            tml += "<tr>" +
                '<td>'+ lists[x]["username"] + '</td>'+
                '<td>'+ lists[x]["nickname"] + '</td>'+
                "<td>" + lists[x]["score"] + "</td>" +
                '<td><a href="javascript:;" class="c-009 f-14" onclick="deleteCourseStudent(this)" data-username="'+ lists[x]["username"] + '">删除</a></td>'+
                "</tr>";
        }
    } else {
        $('#pagination').hide();
    }
    id.innerHTML = tml;
}

// 显示添加用户的版面
function showaddUser(that) {
    $("#addUser").toggle();
    $("#input-adduser").val("");
    if($(that).html()=="添加") {
        that.innerHTML = "取消";
    }
    else {
        that.innerHTML = "添加";
    }
}
function inputTypeChanges(obj) {
    var html_manual='<input type="number" id="input-adduser" class="rad-3 ml-10 f-14"/>'+
        '<a href="javascript:;" class="ui-btn text-center c-fff f-14 bg-169 rad-3 ml-10 " onclick="addCourseStudent()">确认添加</a>';
    var html_files='<input id="filename" type="text" class="rad-3 ml-10 f-14 outline-none c-666 filename" readonly/>'+
        '<div class="upload-box lite-upload-box relative bg-169 text-center c-fff f-14 rad-3 ml-10">'+
        '选择文件<input id="fileInput" type="file" name="files" onchange="fileinput(this)" class="absolute pointer fileinputs"/>'+
        '</div>'+
        '<a href="javascript:;" class="ui-btn text-center c-fff f-14 bg-169 rad-3 ml-10" onclick="addCoursestudentFiles()">上传</a>'+
        '<span class="c-999 ml-10">文件格式 xlsx, csv, xls</span>';
    var way=$(obj).val();
    if(way=='0'){
        $("#addInputs").html(html_manual)
    }
    else {
        $("#addInputs").html(html_files)
    }
}
//添加用户
function addCourseStudent() {
    var addUsername = $("#input-adduser").val();
    $.ajax({
        url: add_courseStudent_interface+"?courseId="+$.getQueryString("courseId"),
        type:'post',
        dataType:'json',
        contentType:"application/json",
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:JSON.stringify({
            courseId:$.getQueryString("courseId"),
            students:[addUsername],
        }),
        success: function(data) {
            if(data.code==200){
                $.message("添加成功");
                courseStudentListFun(pageNow)

            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}
function addCoursestudentFiles() {
    var formdata=new FormData();
    formdata.append("file",$("#fileInput")[0].files[0]);
    console.log($("#fileInput")[0].files[0])
    $.ajax({
        url: add_courseStudent_interface+"?courseId="+$.getQueryString("courseId"),
        type:'post',
        dataType:'json',
        cache: false,
        contentType: false,
        processData: false,
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:formdata,
        success: function(data) {
            if(data.code==200){
                $.message("添加成功");
                studentListFun(pageNow)

            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}
//删除
/**删除***/
function deleteCourseStudent(obj){
    var username = $(obj).attr("data-username");
    $.ajax({
        url: delete_courseStudent_interface,
        type:'post',
        dataType:'json',
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:{
            courseId:$.getQueryString("courseId"),
            studentId:username,
        },
        success: function(data) {
            if(data.code==200){
                $.message("删除成功");
                // $(obj).closest("tr").remove();
                courseStudentListFun(pageNow)
            }else{
                $.message(data.msg);
                courseStudentListFun(pageNow)

            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}